using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Templates;
using Avalonia.Markup.Xaml;
using Avalonia.Data.Core;
using Avalonia.Data.Core.Plugins;
using MathPlus.ViewModels;
using MathPlus.Views;
using Microsoft.Extensions.DependencyInjection;
using MathPlus.Common;
using MathPlus.Features.TextOCR;
using MathPlus.Services;
using MathPlus.Features.Splash;
using MathPlus.Features.LatexOCR;

namespace MathPlus;

public partial class App : Application
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            // Line below is needed to remove Avalonia data validation.
            // Without this line you will get duplicate validations from both Avalonia and CT
            // BindingPlugins.DataValidators.RemoveAt(0);
            // desktop.MainWindow = new MainWindow
            // {
            //     DataContext = new MainWindowViewModel(),
            // };
            var services = new ServiceCollection();

            services.AddSingleton(desktop);

            var views = ConfigureViews(services);
            var provider = ConfigureServices(services);

            DataTemplates.Add(new ViewLocator(views));

            desktop.MainWindow = views.CreateView<MainWindowViewModel>(provider) as Window;
        }

        base.OnFrameworkInitializationCompleted();
    }

     private static SukiViews ConfigureViews(ServiceCollection services)
    {
        return new SukiViews()

            // Add main view
            .AddView<MainWindowView, MainWindowViewModel>(services)

            // Add pages
            .AddView<SplashView, SplashViewModel>(services)
            .AddView<TextOCRView, TextOCRViewModel>(services)
            .AddView<LatexOCRView, LatexOCRViewModel>(services);
            // .AddView<ThemingView, ThemingViewModel>(services)
            // .AddView<PlaygroundView, PlaygroundViewModel>(services)
            // .AddView<EffectsView, EffectsViewModel>(services)
            // .AddView<DashboardView, DashboardViewModel>(services)
            // .AddView<ButtonsView, ButtonsViewModel>(services)
            // .AddView<CardsView, CardsViewModel>(services)
            // .AddView<CollectionsView, CollectionsViewModel>(services)
            // .AddView<ContextMenusView, ContextMenusViewModel>(services)
            // .AddView<DockView, DockViewModel>(services)
            // .AddView<ExpanderView, ExpanderViewModel>(services)
            // .AddView<IconsView, IconsViewModel>(services)
            // .AddView<InfoBarView, InfoBarViewModel>(services)
            // .AddView<MiscView, MiscViewModel>(services)
            // .AddView<ProgressView, ProgressViewModel>(services)
            // .AddView<PropertyGridView, PropertyGridViewModel>(services)
            // .AddView<TextView, TextViewModel>(services)
            // .AddView<TogglesView, TogglesViewModel>(services)
            // .AddView<ToastsView, ToastsViewModel>(services)
            // .AddView<TabControlView, TabControlViewModel>(services)
            // .AddView<StackPageView, StackPageViewModel>(services)
            // .AddView<DialogsView, DialogsViewModel>(services)
            // .AddView<ColorsView, ColorsViewModel>(services)

            // // Add additional views
            // .AddView<DialogView, DialogViewModel>(services)
            // .AddView<VmDialogView, VmDialogViewModel>(services)
            // .AddView<RecursiveView, RecursiveViewModel>(services)
            // .AddView<CustomThemeDialogView, CustomThemeDialogViewModel>(services);
    }

    private static ServiceProvider ConfigureServices(ServiceCollection services)
    {
        // services.AddSingleton<ClipboardService>();
        services.AddSingleton<PageNavigationService>();
        // services.AddSingleton<ISukiToastManager, SukiToastManager>();
        // services.AddSingleton<ISukiDialogManager, SukiDialogManager>();

        return services.BuildServiceProvider();
    }

}